package com.example.qxfw.dzzh.service.impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.yj.DzWarning;
import com.example.qxfw.common.entity.zy.BaseProduct;
import com.example.qxfw.dzzh.mapper.DzWarningMapper;
import com.example.qxfw.dzzh.service.DzzhYjcxService;
import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.ybzz.mapper.BaseProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class DzzhYjcxServiceImpl implements DzzhYjcxService {

    @Autowired
    private DzWarningMapper dzWarningMapper;

    @Autowired
    private BaseProductMapper baseProductMapper;

    @Qualifier("mongoSecondTemplate")
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<DzWarning> getDzWaringList(String start, String end, Integer state) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return dzWarningMapper.selectList(Wrappers.<DzWarning>lambdaQuery()
                .between(DzWarning::getDatechar,sdf.parse(start),sdf.parse(end))
                .eq(ObjectUtils.isNotEmpty(state),DzWarning::getState,state)
                .orderBy(true,false,DzWarning::getDocid));
    }

    @Override
    public DzWarning updateState(Integer docId, Integer state) {
        DzWarning dzWarning = new DzWarning();
        dzWarning.setDocid(docId);
        dzWarning.setState(state);
        dzWarningMapper.updateById(dzWarning);
        return dzWarningMapper.selectById(docId);
    }

    @Override
    public Integer getProductId(Integer docId,String docPath) {
        BaseProduct baseProduct = baseProductMapper.selectOne(Wrappers.<BaseProduct>lambdaQuery()
                .eq(BaseProduct::getBindid, docId)
                .eq(BaseProduct::getProductname, docPath));
        if (baseProduct!=null){
            return baseProduct.getId();
        } else  {
            return null;
        }
    }

    @Override
    public DzWarning deleteProduct(Integer docId) {
        DzWarning deleteDzWaring = dzWarningMapper.selectById(docId);
        dzWarningMapper.deleteById(docId);
        if (deleteDzWaring!=null && ObjectUtils.isNotEmpty(deleteDzWaring.getDocid()) && StringUtils.isNotBlank(deleteDzWaring.getDocpath())){
            BaseProduct baseProduct = baseProductMapper.selectOne(Wrappers.<BaseProduct>lambdaQuery()
                    .eq(BaseProduct::getBindid, deleteDzWaring.getDocid())
                    .eq(BaseProduct::getProductname, deleteDzWaring.getDocpath()));
            if (baseProduct!=null && ObjectUtils.isNotEmpty(baseProduct.getId())){
                baseProductMapper.deleteById(baseProduct.getId());
                mongoTemplate.remove(new Query(
                        Criteria.where("id").is(baseProduct.getId())
                                .and("table_name").is("base_product")), HyDocFile.class);
            }
        }
        return deleteDzWaring;
    }
}
